<?php

namespace Modules\Admin\Http\Controllers;

use Houdunwang\Module\Facade\HdModuleConfig;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Admin\Http\Requests\RoleRequest;
use Spatie\Permission\Models\Role;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
    	//$roles = Role::get();
		$roles = Role::where('name','!=',config('hd_module.webmaster'))->get();
		return view('admin::role.index',compact ('roles'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('admin::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(RoleRequest $request)
    {
    	Role::create ([
    		'name' =>$request->input ('name'),
			'title' =>$request->input ('title'),
		]);
    	session ()->flash ('success','角色添加成功');
    	return redirect ()->route ('role.index');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('admin::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('admin::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(RoleRequest $request,$id)
    {
    	$role = Role::find($id);
    	$role->title = $request->input ('title');
    	$role->name = $request->input ('name');
    	$role->save ();
    	session ()->flash ('success','角色修改成功');
    	return redirect ()->route ('role.index');
    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
		$role = Role::find($id);
		if ($role['name'] == config('hd_module.webmaster')){
			session()->flash('danger','请不要挑战删除网站站长!!!');
			return back();
		}
		Role::destroy ($id);
		session ()->flash ('success','角色删除成功');
		return back ();
    }
    public function  showPermission($roleid){
    	$role = Role::find($roleid);
		$permissions = \HDModule::getPermissionByGuard('admin');
		return view ('admin::role.permission',compact ('permissions','role'));
	}
	public function permission(Request $request,$roleid){
    	if (!$request->input ('name')){
    		session ()->flash ('danger','最少选择一个权限');
    		return back ();
		}
		$role = Role::find($roleid);
		$role->syncPermissions($request->input('name'));
		session ()->flash ('success','权限分配成功');
		return redirect ()->route ('role.index');
	}
}
